package com.zyy.smartcity1129.reservation;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zyy.smartcity1129.data.ReserDBInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: ReserDataAdapter$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 30/11/2021$
 * @Version: 1.0
 */
public class ReserDataAdapter {
    private ReserSQLiteOpenHelper openHelper;
    private SQLiteDatabase database;
    public void openDB(Context context) {
        openHelper = new ReserSQLiteOpenHelper(context, "Reser.db", null, 1);
        try {
            database = openHelper.getWritableDatabase();
        } catch (Exception e) {
            database = openHelper.getReadableDatabase();
        }
    }
    public void closeDB() {
        if (openHelper != null) {
            openHelper.close();
            openHelper = null;
        }
        if (database != null) {
            database.close();
            database = null;
        }
    }
    public List<ReserDBInfo> query(Context context, String mPlateNo) {
        List<ReserDBInfo> list = new ArrayList<>();
        openDB(context);
        Cursor cursor = database.query("Reser", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                String plateNo = cursor.getString(1);
                String time = cursor.getString(2);
                String address = cursor.getString(3);
                if (plateNo.equals(mPlateNo)) {
                    list.add(new ReserDBInfo(plateNo, time, address));
                }
            } while (cursor.moveToNext());
        }
        cursor.close();
        closeDB();
        return list;
    }
    public List<ReserDBInfo> queryAll(Context context) {
        List<ReserDBInfo> list = new ArrayList<>();
        openDB(context);
        Cursor cursor = database.query("Reser", null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(0);
                String plateNo = cursor.getString(1);
                String time = cursor.getString(2);
                String address = cursor.getString(3);
                list.add(new ReserDBInfo(plateNo, time, address));
            } while (cursor.moveToNext());
        }
        cursor.close();
        closeDB();
        return list;
    }
    public long insert(Context context, ReserDBInfo info) {
        openDB(context);
        ContentValues values = new ContentValues();
        values.put("plateNo", info.getPlateNo());
        values.put("time", info.getTime());
        values.put("address", info.getAddress());
        long result = database.insert("Reser", null, values);
        closeDB();
        return result;
    }
    public int delete(Context context, String plateNo) {
        openDB(context);
        int result = database.delete("Reser", "plateNo=?", new String[]{plateNo});
        closeDB();
        return result;
    }
    public void deleteAll(Context context) {
        openDB(context);
        database.execSQL("delete from Reser");
        closeDB();
    }
    public int update(Context context, String plateNo, ReserDBInfo info) {
        openDB(context);
        ContentValues values = new ContentValues();
        values.put("plateNo", info.getPlateNo());
        values.put("time", info.getTime());
        values.put("address", info.getAddress());
        int result = database.update("Reser", values, "plateNo=?", new String[]{plateNo});
        closeDB();
        return result;
    }
}
